import {
  Manager,
  ManagerSaveParams,
  ManagerSearchParams,
  ManagerUpdateParams
} from '@/api/manager/model';
import request from '@/utils/request';
import { ApiResult } from '@/api';

/**
 * 获取平台管理员信息
 */
export async function getManagerInfo(): Promise<Manager> {
  const res = await request.get<ApiResult<Manager>>('/employee/info');
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加平台管理员
 * @param data
 */
export async function addManager(data: ManagerSaveParams) {
  const res = await request.post<ApiResult<unknown>>('/manager', data);
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 更新平台管理员
 * @param data
 */
export async function updateManager(data: ManagerUpdateParams) {
  const res = await request.put<ApiResult<unknown>>('/manager', data);
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取管理员列表
 * @param data
 */
export async function getManagerList(data: ManagerSearchParams) {
  const res = await request.get<ApiResult<Manager>>('/manager', {
    params: data
  });
  if (res.data.code === 0) {
    const pageData = {
      list: res.data.data
    };
    return pageData;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改管理员状态
 * @param id
 * @param status
 */
export async function updateManagerStatus(id?: string, status?: boolean) {
  const res = await request.patch<ApiResult<unknown>>(
    '/manager/status',
    {},
    {
      params: {
        id: id,
        status: status
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
